﻿using System.Collections.Generic;
using Common;
using Entities;
using Microsoft.AspNetCore.Mvc;
using Portal.Models;
using SqlSugar;
using WMS.DtoModels;

namespace Portal.Controllers
{
    public class ProviderController : Controller
    {
        private readonly SqlSugarClient _sqlSugarClient;

        public ProviderController(SqlSugarClient sqlSugarClient)
        {
            _sqlSugarClient = sqlSugarClient;
        }

        /// <summary>
        /// 根据条件加载供应商列表
        /// </summary>
        /// <param name="providerInfoReqDto"></param>
        /// <returns></returns>
        public IActionResult LoadAllProvider(ProviderInfoReqDto providerInfoReqDto)
        {
            var totalCount = 0;
            var querySqlQueryable = this._sqlSugarClient.Context.Queryable<bus_provider>();
            querySqlQueryable = querySqlQueryable.Where(providerInfoReqDto);
            var queryResult = querySqlQueryable.ToPageList(providerInfoReqDto.Page, providerInfoReqDto.Limit, ref totalCount);

            var result = new
            {
                Code = 0,
                Msg = "查询成功",
                Count = totalCount,
                Data = queryResult
            };

            return Json(result);
        }

        public IActionResult LoadAllProviderForSelect()
        {
            var queryResult = this._sqlSugarClient.Context.Queryable<bus_provider>().ToList();
            var result = new
            {
                Code = 0,
                Msg = "查询成功",
                Count = queryResult.Count,
                Data = queryResult
            };

            return Json(result);
        }

        /// <summary>
        /// 添加提供商
        /// </summary>
        /// <param name="busProvider">需要添加的供应商信息</param>
        /// <returns></returns>
        public IActionResult AddProvider(bus_provider busProvider)
        {
            var updateNum = this._sqlSugarClient.Context.Insertable<bus_provider>(busProvider).ExecuteCommand();
            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功添加{updateNum}个提供商信息"
            };

            return Json(result);
        }

        /// <summary>
        /// 根据id删除供应商信息
        /// </summary>
        /// <param name="id">供应商id</param>
        /// <returns></returns>
        public IActionResult DeleteProvider(int id)
        {
            var deleteNum = this._sqlSugarClient.Deleteable<bus_provider>(x => x.id == id).ExecuteCommand();
            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功删除{deleteNum}个供应商信息"
            };

            return Json(result);
        }

        /// <summary>
        /// 更新提供商信息
        /// </summary>
        /// <param name="busProvider">需要更新的供应商信息</param>
        /// <returns></returns>
        public IActionResult UpdateProvider(bus_provider busProvider)
        {
            var updateNum = this._sqlSugarClient.Context.Updateable<bus_provider>(busProvider).ExecuteCommand();
            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功更新{updateNum}个提供商信息"
            };

            return Json(result);
        }

        /// <summary>
        /// 根据主键批量删除供应商信息
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public IActionResult BatchDeleteProvider(List<int> ids)
        {
            var deleteNum = this._sqlSugarClient.Deleteable<bus_provider>().In(ids).ExecuteCommand();
            var result = new LayUiStandardResDto<object>
            {
                Code = GlobalVariable.Ok,
                Msg = $"成功删除{deleteNum}个供应商信息"
            };

            return Json(result);
        }


    }
}